package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.ComposePathEffect
import android.graphics.CornerPathEffect
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* MirrorReversalView
* */
class MirrorReversalView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mCornerValue = context.resources.getDimension(R.dimen.dp_3)

    private var mCornerPathEffect: CornerPathEffect? = null
    private var mComposePathEffect: ComposePathEffect? = null

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_FFAB40)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            pathEffect = CornerPathEffect(mCornerValue)
            color = context.resources.getColor(R.color.color_FFAB40)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            val mStrokeWidth = (cx / 15).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth

            if (mCornerPathEffect == null) {
                mCornerPathEffect = CornerPathEffect(mCornerValue)
            }

            mPath0.apply {
                reset()
                moveTo(-cx * 11 / 12, -cy * 11 / 12)
                lineTo(-cx * 1 / 24, -cy * 7 / 12)
                lineTo(-cx * 1 / 24, cy * 7 / 12)
                lineTo(-cx * 11 / 12, cy * 11 / 12)
                close()
            }
            mPaint0.pathEffect = mCornerPathEffect
            drawPath(mPath0, mPaint0)

            mPath0.apply {
                reset()
                moveTo(cx * 1 / 12, -cy * 7 / 12)
                lineTo(cx * 11 / 12, -cy * 11 / 12)
                lineTo(cx * 11 / 12, cy * 11 / 12)
                lineTo(cx * 1 / 12, cy * 7 / 12)
                close()
            }
            if (mComposePathEffect == null) {
                val distance = cy * 11 / 12 * 2 + cy * 7 / 12 * 2 + Math.sqrt((cx * 10 / 12 * cx * 10 / 12 + cy * 4 / 12 * cy * 4 / 12).toDouble()).toFloat() * 2
                val dashSolid = distance / 16
                val mDashPathEffect = DashPathEffect(floatArrayOf(dashSolid * 2 / 3, dashSolid * 1 / 3), 0F)
                mComposePathEffect = ComposePathEffect(mDashPathEffect, mCornerPathEffect)
            }
            mPaint1.strokeWidth = mStrokeWidth
            mPaint1.pathEffect = mComposePathEffect
            drawPath(mPath0, mPaint1)

            restore()
        }
    }
}